﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evan.WebUI
{
    public class DetailManager : ManagerPage
    {
        protected int Id = 0;
        protected string TableName = "";
        
        protected dynamic Model = new Model.DynamicModel();
        protected List<Evan.Model.SysModel.HtmlHelperModel> Html_List = new List<Model.SysModel.HtmlHelperModel>();
        public DetailManager()
        {
            this.Load += new EventHandler(DetailManager_Load);
        }

        private void DetailManager_Load(object sender, EventArgs e)
        {
            int.TryParse(Common.CommFun.GetQueryString("id"), out Id);
            int.TryParse(Common.CommFun.GetQueryString("classid"), out ClassId);
            TableName = Common.CommFun.GetQueryString("t");
            if (string.IsNullOrEmpty(TableName))
            {
                throw new Exception("参数 \"t\" 不能为空");
            }

            if (TableName == "Versions") {
                Evan.DataBase.DbOperat BLL_Versions = new Evan.DataBase.DbOperat("Versions");
                Model = BLL_Versions.GetModel(Convert.ToInt32(Common.CommFun.GetAppSetting("VerId")));
            }
            else if (TableName == "SinglePage")
            {
                //单页
                if (ClassId > 0)
                {
                    Evan.DataBase.DbOperat BLL_SinglePage = new Evan.DataBase.DbOperat("SinglePage");
                    Model = BLL_SinglePage.GetModel_1(string.Format("ClassId={0}", ClassId));
                    if (Model == null)
                    {
                        Model = new Evan.Model.DynamicModel();
                        Model.Set("ClassId", ClassId);
                        Model = BLL_SinglePage.InsertReturnModel(Model);
                        if (!Evan.Model.ConvertModel.HasKey(Model, "Title"))
                        {
                            Model.Set("Title", "");
                        }
                    }
                }
            }
            else
            {
                //非单页
                if (Id > 0)
                {
                    Model = new Evan.DataBase.DbOperat(TableName).GetModel(Id);
                }
                else
                {
                    Model = null;
                    // 2017年11月23日17:04:30 这里本来没有  新增后暂时不用 , 如果有需要可以修改本部分
                    //Model = new Evan.Model.DynamicModel();
                    //Model.Set("ClassId", ClassId);
                    //if (!Evan.Model.ConvertModel.HasKey(Model, "Title"))
                    //{
                    //    Model.Set("Title", "");
                    //}
                }
            }

            if (ClassId > 0)
            {
                List<dynamic> Field_List = new Evan.DataBase.DbOperat("Fields").GetList(string.Format("ClassId={0} and States=1", ClassId),"SortId asc,Id asc");
                foreach (var item in Field_List)
                {
                    Html_List.Add(Common.HtmlHelper.ConvertToHtmlHelperModel(item));
                }
            }

        }
    }
}
